/*
 * WSFrontend.java
 *
 * Created on April 29, 2007, 11:59 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.javatech.jmf.server;

import info.javatech.jmf.common.ProtocolConstants;
import info.javatech.jmf.common.components.CommandConstants;
import info.javatech.jmf.server.protocol.PCommand;
import info.javatech.jmf.server.protocol.PComponent;
import info.javatech.jmf.server.protocol.PRequest;
import info.javatech.jmf.server.protocol.PRequestProcessException;
import info.javatech.jmf.server.protocol.PResponse;
import info.javatech.jmf.server.protocol.PScreen;
import info.javatech.jmf.server.protocol.PScreenContainer;
import info.javatech.jmf.server.util.EncodeUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.IOException;
import javax.faces.render.ResponseStateManager;
import javax.faces.webapp.FacesServlet;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.ws.WebFault;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.w3c.dom.Document;

/**
 *
 * @author afedotov
 */
@WebService
public class WSFrontend {
    /**
     * Web service operation
     */
    @WebMethod
    public PResponse doRequest(PRequest request) throws PRequestProcessException{
        
        if (request==null)
            throw new PRequestProcessException(PRequestProcessException.UNPROPER_INPUT_PARAMETER);
        
        try {
            HttpClient client = new HttpClient();
            String url = "http://localhost:8080"+
                    request.getViewId()+
                    (request.getSessionId()!=null?";jsessionid="+request.getSessionId():"");
            PostMethod method = new PostMethod(url);
            try {
                if (request.getComponents()!=null&&request.getSessionId()!=null) {
                    for(PComponent component:request.getComponents()) {
                        method.addParameter(component.getId(),EncodeUtils.encodeToString(component));
                    }
                    method.addParameter(ResponseStateManager.VIEW_STATE_PARAM,request.getViewState());
                }
                
                client.executeMethod(method);
                
                if (method.getStatusCode()==200) {
                    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
                    Document doc = bf.newDocumentBuilder().parse(method.getResponseBodyAsStream());
                    
                    PResponse resp = (PResponse) EncodeUtils.decode(doc.getFirstChild().getTextContent().getBytes());
                    
                    return resp;
                } else {
                    //TODO need to clarify system behavior in case of non-200 response statuses
                    return null;
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                
                return null;
            } finally {
                method.releaseConnection();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new PRequestProcessException(PRequestProcessException.UNKNOWN_ERROR);
        }
        
/*
        PCommand command = new PCommand();
        command.setPropertyNames(new String[]{CommandConstants.LONG_TITLE_VALUE_NAME,CommandConstants.SHORT_TITLE_VALUE_NAME, CommandConstants.TYPE_VALUE_NAME, CommandConstants.PRIORITY_VALUE_NAME});
        command.setPropertyValues(new String[]{"Long Command title","Short command title", "1", "1"});
        command.setType("command");
 
        PCommand command2 = new PCommand();
        command2.setPropertyNames(new String[]{CommandConstants.LONG_TITLE_VALUE_NAME,CommandConstants.SHORT_TITLE_VALUE_NAME, CommandConstants.TYPE_VALUE_NAME, CommandConstants.PRIORITY_VALUE_NAME});
        command2.setPropertyValues(new String[]{"Long Command title 2","Short command title 2", "2", "2"});
        command2.setType("command");
 
        PResponse resp = new PResponse();
        PScreenContainer cont = new PScreenContainer();
        resp.setViewType(ComponentConstants.ROOT_COMPONENT_KIND_SCREEN_CONTAINER);
        cont.setType("form");
        cont.setPropertyNames(new String[]{"title"});
        cont.setPropertyValues(new String[]{"formTitle"});
        cont.setCommands(new PCommand[]{command, command2});
        resp.setScreenContainer(cont);
 
        return resp;
 */
    }
}
